#include <QSettings>
#include <QMessageBox>
#include <QTextCodec>
#include "cdrvsyncbasecfg.h"
#include "ui_cdrvsyncbasecfg.h"
#include "cglobal.h"
#include "cpublicdata.h"

CDrvSyncBaseCfg::CDrvSyncBaseCfg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CDrvSyncBaseCfg)
{
    ui->setupUi(this);
    initConnect();
    initUi();
}

CDrvSyncBaseCfg::~CDrvSyncBaseCfg()
{
    delete ui;
}

void CDrvSyncBaseCfg::initConnect()
{
    connect(ui->pushButtonOK, &QPushButton::clicked, this, &CDrvSyncBaseCfg::slotPushButtonOK);
    connect(ui->pushButtonCancle, &QPushButton::clicked, this, &CDrvSyncBaseCfg::slotPushButtonCancle);
}

void CDrvSyncBaseCfg::initUi()
{
    this->setWindowTitle(A2T("驱动配置"));
    ui->lineEditSyncBaseIP->setText(CPublicData::instance().m_stSyncBaseCfg.m_sIp);
    ui->lineEditSyncBasePort->setText(QString::number(CPublicData::instance().m_stSyncBaseCfg.m_nPort));
    ui->lineEditSyncBaseUser->setText(CPublicData::instance().m_stSyncBaseCfg.m_sUser);
    ui->lineEditSyncBasePwd->setText(CPublicData::instance().m_stSyncBaseCfg.m_sPassWd);
    ui->lineEditSyncBaseDbName->setText(CPublicData::instance().m_stSyncBaseCfg.m_sDbName);
}

void CDrvSyncBaseCfg::slotPushButtonOK()
{
    int ret = QMessageBox::information(this, A2T("提示"), A2T("是否确认保存?"), QMessageBox::Yes, QMessageBox::No);
    if (ret == QMessageBox::No)
    {
        return;
    }
    CPublicData::instance().m_stSyncBaseCfg.m_sIp=ui->lineEditSyncBaseIP->text();
    CPublicData::instance().m_stSyncBaseCfg.m_nPort=ui->lineEditSyncBasePort->text().toInt();
    CPublicData::instance().m_stSyncBaseCfg.m_sUser=ui->lineEditSyncBaseUser->text();
    CPublicData::instance().m_stSyncBaseCfg.m_sPassWd=ui->lineEditSyncBasePwd->text();
    CPublicData::instance().m_stSyncBaseCfg.m_sDbName=ui->lineEditSyncBaseDbName->text();

    QString sIniPath = QCoreApplication::applicationDirPath() + "/config/config.ini";
    QSettings settings(sIniPath, QSettings::IniFormat);
    settings.setIniCodec(QTextCodec::codecForName("UTF-8"));

    settings.beginGroup("SyncBase");
    settings.setValue("IP",CPublicData::instance().m_stSyncBaseCfg.m_sIp);
    settings.setValue("Port", CPublicData::instance().m_stSyncBaseCfg.m_nPort);
    settings.setValue("User", CPublicData::instance().m_stSyncBaseCfg.m_sUser);
    settings.setValue("PassWd", CPublicData::instance().m_stSyncBaseCfg.m_sPassWd);
    settings.setValue("DbName", CPublicData::instance().m_stSyncBaseCfg.m_sDbName);
    settings.endGroup();

    messageBox(0,A2T("保存成功"));
    this->close();
}

void CDrvSyncBaseCfg::slotPushButtonCancle()
{
    this->close();
}
